package cn.tedu.boot53.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {

        String filename = picFile.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
        filename= UUID.randomUUID()+suffix;
        System.out.println("文件名:"+filename);

        String dirPath = "D:/file";
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdir();
        }

        String filePath = dirPath+"/"+filename;
        picFile.transferTo(new File(filePath));
        return "/"+filename;
    }

    @RequestMapping("/remove")
    public void remove(String name){
        String dirPath = "D:/file";
        String file = dirPath+name;
        System.out.println("删除文件的路径:"+file);
        new File(file).delete();
    }
}
